/*
 * Copyright (c) 2022 Kirky
 * Licensed under the Open Software Licence, Version 3.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://opensource.org/licenses/osl-3.0.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lingeringdream.luciole.slice;

import com.lingeringdream.luciole.ResourceTable;
import com.lingeringdream.luciole.helper.ResourceHelper;
import com.lingeringdream.luciole.model.Constants;
import com.lingeringdream.luciole.model.entity.Classification;
import com.lingeringdream.luciole.model.helper.ResultSetHelper;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.DataAbilityHelper;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.utils.TextAlignment;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.data.resultset.ResultSet;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.net.Uri;

import java.util.Locale;

/**
 * 详情页面
 *
 * @author Kirky
 * @version 1.0
 * @date 2022/5/3
 */
public class DetailSlice extends AbilitySlice {
    private final HiLogLabel TAG = new HiLogLabel(HiLog.LOG_APP, 0x13, DetailSlice.class.getSimpleName());
    private Classification classification;
    private boolean isChinese;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setUIContent(ResourceTable.Layout_slice_detail);
        DataAbilityPredicates predicates = new DataAbilityPredicates();
        long cid = intent.getLongParam("cid", -1L);
        if (cid != -1L) {
            predicates.equalTo("cid", cid);
        } else {
            String key = intent.getStringParam("key");
            boolean isChinese = intent.getBooleanParam("isChinese", true);
            if (isChinese) {
                predicates.like("cname", key);
            } else {
                predicates.like("ename", key);
            }
        }

        getItem(predicates);

        if (classification != null) {
            initTitleBar();
            initImage();
            initDetail();
        } else {
            HiLog.error(TAG, "classification is null! ");
        }
    }

    @Override
    protected void onActive() {
        super.onActive();
        isChinese = "zh".equals(Locale.getDefault().getLanguage());
    }

    private void getItem(DataAbilityPredicates predicates) {
        DataAbilityHelper helper = DataAbilityHelper.create(this);
        try {
            ResultSet resultSet = helper.query(Uri.parse(Constants.TABLE_CLASS.getValue()), null, predicates);
            if (resultSet == null || resultSet.getRowCount() == 0) {
                HiLog.info(TAG, "query: resultSet is null or no result found");
                return;
            }
            resultSet.goToFirstRow();
            classification = ResultSetHelper.resultSet2Classification(resultSet);

        } catch (Exception e) {
            HiLog.error(TAG, "query: " + e.getMessage());
        } finally {
            helper.release();
        }
    }

    private void initTitleBar() {
        Button backButton = findComponentById(ResourceTable.Id_titleBar_back);
        backButton.setClickedListener(component -> terminate());

        Text title = findComponentById(ResourceTable.Id_titleBar_title);
        if (isChinese) {
            title.setText(classification.getcName());
        } else {
            title.setText(classification.geteName());
        }
    }

    /**
     * 初始化类别 image
     */
    private void initImage() {
        Image image = findComponentById(ResourceTable.Id_detail_image);
        if (classification.getClazz() == 0) {
            image.setPixelMap(ResourceTable.Media_recyclable);
        } else if (classification.getClazz() == 1) {
            image.setPixelMap(ResourceTable.Media_food);
        } else if (classification.getClazz() == 2) {
            image.setPixelMap(ResourceTable.Media_other);
        } else if (classification.getClazz() == 3) {
            image.setPixelMap(ResourceTable.Media_harmful);
        }
    }

    /**
     * 初始化详情
     */
    private void initDetail() {
        DirectionalLayout detail = findComponentById(ResourceTable.Id_detail_detail);
//       名称
        Component component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        Text title = component.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("物品名称");
        Text content = component.findComponentById(ResourceTable.Id_detail_item_content);
        if (isChinese) {
            content.setText(classification.getcName());
        } else {
            content.setText(classification.geteName());
        }
        detail.addComponent(component);
//        类别
        component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item, null, false);
        title = component.findComponentById(ResourceTable.Id_detail_item_title);
        title.setText("垃圾类别");
        content = component.findComponentById(ResourceTable.Id_detail_item_content);
        if (classification.getClazz() == 0) {
            content.setText(ResourceHelper.getString(getContext(), ResourceTable.String_recyclable, "可回收垃圾", null));
        } else if (classification.getClazz() == 1) {
            content.setText(ResourceHelper.getString(getContext(), ResourceTable.String_food, "厨余垃圾", null));
        } else if (classification.getClazz() == 2) {
            content.setText(ResourceHelper.getString(getContext(), ResourceTable.String_other, "其他垃圾", null));
        } else if (classification.getClazz() == 3) {
            content.setText(ResourceHelper.getString(getContext(), ResourceTable.String_harmful, "有害垃圾", null));
        }
        detail.addComponent(component);
//        建议
        if (classification.getSuggestion() != null) {
            component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item_long, null, false);
            title = component.findComponentById(ResourceTable.Id_detail_item_long_title);
            title.setText("投放建议");
            content = component.findComponentById(ResourceTable.Id_detail_item_long_content);
            content.setText(classification.getSuggestion());
            detail.addComponent(component);
        }
//        危害
        if (classification.getHarmful() != null) {
            component = LayoutScatter.getInstance(this).parse(ResourceTable.Layout_layout_detail_item_long, null, false);
            title = component.findComponentById(ResourceTable.Id_detail_item_long_title);
            title.setText("对环境危害");
            content = component.findComponentById(ResourceTable.Id_detail_item_long_content);
            content.setText(classification.getSuggestion());
            detail.addComponent(component);
        }
//        反馈问题
        Text feedback = new Text(this);
        feedback.setLayoutConfig(new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_PARENT,DirectionalLayout.LayoutConfig.MATCH_CONTENT
        ));
        feedback.setText("反馈问题");
        feedback.setTextSize((int)ResourceHelper.getFloat(this, ResourceTable.Float_textSize_body1,16f));
        feedback.setTextAlignment(TextAlignment.CENTER);
        feedback.setClickedListener(component1 -> {
//            TODO 调转反馈页面
            
        });
        detail.addComponent(feedback);
    }
}
